<?php
    @session_start();
    require_once 'shared-functions.php';
    require_once 'utility-functions.php';
    require_once 'session.php';
    require_once 'masterpage.php';
    
    if(isset($_GET['logout']) && $_GET['logout'] == 1)
    {
        SessionCleanup();
    }

    masterpage("Backups");

    if (isset($_POST['doBackup']))
    {
        if (!is_writable($db_directory))
        {
            echo 'ERROR: Cannot create backup. The backup directory, "'.
                 $db_directory . '", is not writable <br />'; 
        }
        else
        {
            $filename = gen_backup_sql_filename("data");
            $backupContent = backup_db_contents($db_admin_username,
                               $db_admin_password,
                               $filename);
        }
    }
    else if (isset($_POST['doSchema']))
    {
        if (!is_writable($schemadir))
        {
            echo 'ERROR: the schema directory, "'. $schemadir .
                 '", is not writable by the web server<br />'; 
        }
        else if (file_exists($schemafile)  && !is_writable($schemafile))
        {
            echo 'ERROR: the schema file "'. $schemafile . ' cannot be overwritten<br />'; 
        }
        else
        {
            $backupSchema = backup_db_schema($db_admin_username, $db_admin_password);
        }
    }
?>
<script type="application/javascript">
   function blah()
   {
       

   }

      
</script>
    <form name="backupform" action="backup.php" method="POST">
        <table class="DataListr">
            <tr><th class="header" colspan="3" >Recent backup files</th></tr>
            <?php
                $bk_dir = opendir($db_directory);
                $rowID = false;
                while ($cur_filename = readdir($bk_dir))
                {
                    if (preg_match("%-data-([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])-([0-9][0-9][0-9][0-9]).sql%", $cur_filename, $matches))
                    {    
                        $matchvar = $matches[1] .$matches[2] . $matches[3] .$matches[4];
                        if ($rowID)
                        {
                            $rowclass = 'altrows';
                            $rowID = false;
                        }
                        else
                        {
                            $rowclass = 'rows';
                            $rowID = true;
                        }

                        if (isset($_POST[$matchvar]))
                        {
                            if ($_POST[$matchvar] == 'Delete')
                            {
                                unlink($db_directory.'/'.$cur_filename);
                            }
                            else if ($_POST[$matchvar] == 'Restore')
                            {
                                $restore = $db_directory.'/'.$cur_filename;
                                echo '<tr><td colspan="3">Restoring backup ' .
                                    $matchvar . '</td></tr>';
                            }
                        }
                        else
                        {

                            echo '<tr class="' . $rowclass .
                                  '"><td>year: ' . $matches[1] .
                                  ', month: ' . $matches[2] .
                                  ', day: ' . $matches[3] .
                                  ', time: ' . $matches[4] . '</td>
                                   <td>
                                     <input type="submit" name="' . $matchvar . '" value="Restore">
                                   </td>
                                   <td>
                                     <input type="submit" name="' . $matchvar . '" value="Delete">
                                   </td>
                                 </tr>';
                        }
                    }
		}
            ?>
        </table>
        <br />
        Backup All System Data <input type="submit" name="doBackup" value="Create Backup" /><br />
        Backup System Schema <input type="submit" name="doSchema" value="Create Backup" /><br />
    </form>         
<?php
    if (isset($restore))
    {
        //wipe out any existing data
        $reloadScheme = reload_schema($db_admin_username, $db_admin_password);
        echo '<br />';
        $runDbScript = run_db_script($db_admin_username, $db_admin_password, $restore);
            
    }
    endmasterpage();

?>
